package com.gaussian.property.admin.module.system.controller.monitor;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.songqi.common.core.domain.R;
import com.songqi.common.core.domain.page.PageDataInfo;
import com.songqi.common.log.annotation.Log;
import com.gaussian.property.module.system.enums.BusinessType;
import com.songqi.common.web.request.controller.BaseController;
import com.gaussian.property.module.system.entity.SysLoginInfo;
import com.gaussian.property.module.system.service.ISysLoginInfoService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统访问记录
 *
 * @author songqi(13776346982)
 */
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLogininforController extends BaseController
{
    @Resource
    private ISysLoginInfoService logininforService;

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @GetMapping("/list")
    public R<PageDataInfo> list(SysLoginInfo logininfor)
    {
        startPage();
        List<SysLoginInfo> list = logininforService.selectLoginInfoList(logininfor);
        return getDataTable(list);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @GetMapping("/export")
    public R export(SysLoginInfo logininfor)
    {
        List<SysLoginInfo> list = logininforService.selectLoginInfoList(logininfor);
        Workbook workBook = ExcelExportUtil.exportExcel(new ExportParams(), SysLoginInfo.class, list);
        //TODO: 日志下载
        return R.success();
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public R remove(@PathVariable Long[] infoIds)
    {
        return toAjax(logininforService.deleteLoginInfoByIds(infoIds));
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public R clean()
    {
        logininforService.cleanLoginInfo();
        return success();
    }
}
